package com.xxgc.mybatis.controller;

import com.xxgc.mybatis.po.User;
import com.xxgc.mybatis.service.UserService;
import com.xxgc.mybatis.service.impl.UserServiceImpl;
import com.xxgc.mybatis.vo.RespBean;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户注册接口
 * */
@WebServlet("/register")
public class UserRegisterController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject jsonObject = new JSONObject();
        System.out.println("进来了");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        //获取参数
        String uName = req.getParameter("uName");
        String uPassword = req.getParameter("uPassword");
        String uSex = req.getParameter("uSex");
        String uNickName = req.getParameter("uNickName");
        if(uName ==null || uName.length()>10){
            jsonObject.put("result",RespBean.error("用户名输入有误"));
            resp.getWriter().write(jsonObject.toString());
        }else if(uPassword == null || uPassword.length()>15){
            jsonObject.put("result",RespBean.error("密码输入有误"));
            resp.getWriter().write(jsonObject.toString());
        }else if(uNickName == null || uNickName.length()>15){
            jsonObject.put("result",RespBean.error("昵称输入有误"));
            resp.getWriter().write(jsonObject.toString());
        }
        User user = new User();
        user.setUName(uName);
        user.setUPassword(uPassword);
        user.setUSex(uSex);
        user.setUNickName(uNickName);
        System.out.println(user);
        RespBean result = userService.userRegister(user);
        System.out.println(result);

        jsonObject.put("result",result);
        resp.getWriter().write(jsonObject.toString());
    }
}
